#include "objCommon.h"

// 销毁userdata结构体
void ui_destroy_userdata(lv_obj_t *obj)
{
    custom_data_t *user_data = (custom_data_t *)lv_obj_get_user_data(obj);
    if (user_data)
    {
        free(user_data);
    }
}


// 创建userdata结构体
custom_data_t *ui_create_userdata(void)
{
    return (custom_data_t *)malloc(sizeof(custom_data_t));
}

uint32_t COMMON_LV_EVENT_GET_COMP_CHILD = _LV_EVENT_LAST + 1;

typedef struct {
    uint32_t child_idx;
    lv_obj_t * child;
    lv_obj_t ** parent;
} common_ui_comp_get_child_t;

lv_obj_t * common_ui_comp_get_child(lv_obj_t * comp, uint32_t child_idx)
{
    common_ui_comp_get_child_t info;
    info.child = NULL;
    info.child_idx = child_idx;
    lv_event_send(comp, COMMON_LV_EVENT_GET_COMP_CHILD, &info);
    return info.child;
}

lv_obj_t ** common_ui_comp_get_parent(lv_obj_t * comp)
{
    common_ui_comp_get_child_t info;
    info.child = NULL;
    info.child_idx = 0xFFFFFFFF;
    lv_event_send(comp, COMMON_LV_EVENT_GET_COMP_CHILD, &info);
    return info.parent;
}

void common_get_component_child_event_cb(lv_event_t * e)
{
    lv_obj_t ** c = lv_event_get_user_data(e);
    common_ui_comp_get_child_t * info = lv_event_get_param(e);
    if(info->child_idx == 0xFFFFFFFF){
        info->parent = c;
        return;
    }
    info->child = c[info->child_idx];
}

void common_del_component_child_event_cb(lv_event_t * e)
{
    // printf("del_component_child_event_cb\n");
    lv_obj_t ** c = lv_event_get_user_data(e);
    LV_LOG_USER("common_del_component_child_event_cb");
    // printf("del_component_child_event_cb: %p\n", c);
    lv_mem_free(c);
}

void common_del_obj_user_data_event_cb(lv_event_t * e)
{
    // printf("del_component_child_event_cb\n");
    lv_obj_t *obj = lv_event_get_target(e);
    void * user_data = lv_obj_get_user_data(obj);
    LV_LOG_USER("common_del_obj_user_data_event_cb");
    // printf("del_component_child_event_cb: %p\n", c);
    if(user_data){
        // LV_LOG_USER("user_data:%s",user_data);
//        lv_mem_free(user_data);
        free(user_data);

    }
}


void common_del_event_data_event_cb(lv_event_t * e){
    LOG_INFO_WRAPPER("start");
    void * data = lv_event_get_user_data(e);
    if(data != NULL){
        free(data);
    }
}

void draw_dashed_borders(lv_obj_t *obj, uint16_t width, uint16_t height) {
    // lv_coord_t x = lv_obj_get_x(obj);
    // lv_coord_t y = lv_obj_get_y(obj);
    // lv_area_t area;
    // lv_obj_get_coords(obj,&area);
    // LV_LOG_USER("area.x1:%d,area.y1:%d,area.x2:%d,area.y2:%d",area.x1,area.y1,area.x2,area.y2);
    // LV_LOG_USER("x:%d,y:%d",x,y);

    // 创建虚线样式
    static lv_style_t dashed_line_left_style;
    lv_style_init(&dashed_line_left_style);
    lv_style_set_line_width(&dashed_line_left_style, 1); // 设置线宽为2像素
    lv_style_set_line_color(&dashed_line_left_style, lv_color_black()); // 设置线颜色为黑色
    lv_style_set_line_dash_width(&dashed_line_left_style, 2); // 设置虚线的线段长度
    lv_style_set_line_dash_gap(&dashed_line_left_style, 1); // 设置虚线的间隔长度

    // 创建虚线样式
    static lv_style_t dashed_line_right_style;
    lv_style_init(&dashed_line_right_style);
    lv_style_set_line_width(&dashed_line_right_style, 1); // 设置线宽为2像素
    lv_style_set_line_color(&dashed_line_right_style, lv_color_black()); // 设置线颜色为黑色
    lv_style_set_line_dash_width(&dashed_line_right_style, 1); // 设置虚线的线段长度
    lv_style_set_line_dash_gap(&dashed_line_right_style, 2); // 设置虚线的间隔长度

    // 上边框
    lv_obj_t *top_border = lv_line_create(obj);
    static lv_point_t top_border_points[] = {{0, 0}, {0, 0}};
    top_border_points[1].x = width;
    lv_line_set_points(top_border, top_border_points, 2);
    lv_obj_add_style(top_border, &dashed_line_right_style, 0);
    lv_obj_align(top_border,LV_ALIGN_TOP_LEFT,0,0);
    // lv_obj_set_pos(top_border, -2, -2);

    // 下边框
    lv_obj_t *bottom_border = lv_line_create(obj);
    static lv_point_t bottom_border_points[] = {{0, 0}, {0, 0}};
    bottom_border_points[0].y = height;
    bottom_border_points[1].x = width;
    bottom_border_points[1].y = height;
    lv_line_set_points(bottom_border, bottom_border_points, 2);
    lv_obj_add_style(bottom_border, &dashed_line_right_style, 0);
    lv_obj_align(bottom_border,LV_ALIGN_TOP_LEFT,0,0);

    // lv_obj_set_pos(bottom_border, -2, -2);

    // 左边框
    lv_obj_t *left_border = lv_line_create(obj);
    static lv_point_t left_border_points[] = {{0, 0}, {0, 0}};
    left_border_points[1].y = height;
    lv_line_set_points(left_border, left_border_points, 2);
    lv_obj_add_style(left_border, &dashed_line_left_style, 0);
    lv_obj_align(left_border,LV_ALIGN_TOP_LEFT,0,0);

    // lv_obj_set_pos(left_border, -2, -2);

    // 右边框
    lv_obj_t *right_border = lv_line_create(obj);
    static lv_point_t right_border_points[] = {{0, 0}, {0, 0}};
    right_border_points[0].x = width;
    right_border_points[1].x = width;
    right_border_points[1].y = height;
    lv_line_set_points(right_border, right_border_points, 2);
    lv_obj_add_style(right_border, &dashed_line_left_style, 0);
    lv_obj_align(right_border,LV_ALIGN_TOP_LEFT,0,0);

    // lv_obj_set_pos(right_border, -2, -2);
}

void ui_update_top_padding(lv_obj_t* obj,lv_coord_t top_padding){

    lv_coord_t pad_top =lv_obj_get_style_pad_top(obj, LV_PART_MAIN | LV_STATE_DEFAULT);
    LV_LOG_USER("pad_top:%d",pad_top);
    lv_obj_set_style_pad_top(obj, pad_top+top_padding, LV_PART_MAIN | LV_STATE_DEFAULT);
}

void ui_screen_group_deinit_common(lv_obj_t **screen,lv_group_t **group){
    LOG_INFO_WRAPPER("start");
    if(group != NULL){
        if(*group != NULL){
            lv_group_del(*group);
            *group = NULL;
        }
    }
    if(screen!= NULL){
        if(*screen != NULL){
            lv_obj_clean(*screen);
            lv_obj_del(*screen);
            *screen = NULL;
        }
    }
    LOG_INFO_WRAPPER("end");
}

void * malloc_set_zero(uint32_t size){
    void * p = malloc(size);
    if(p != NULL){
        memset(p,0,size);
    }
    return p;
}